#ifndef IFSM_H
#define IFSM_H
#pragma pack(push,1)

#include "../assetfilemgr.h"
#include "../formats/fsmformat.h"

class IFSM : public CFSMDef
{
public:
	static void	*AssetInit( IAssetFile *pFile, const CMasterIndexEntry *pEntry, void *context );
	static void	AssetUninit( void *ptr, void *context );
	static void	AssetFixup( CAssetFileMgr *pMgr, const IAssetFile *pFile, void *ptr, const CMasterIndexEntry *pEntry );

	static void	Register( CAssetFileMgr *pMgr )
	{
		pMgr->AddAssetType("FSM",AssetInit,AssetUninit,AssetFixup);
	}

public:

	const char			*GetText( HASH nameHash ) const
	{
		const char		*ret;
		const IFSM		*pfsm = this;

		while( pfsm )
		{
			ret = (const char *)pfsm->dictionary.FindByHash(nameHash);
			if( ret )
				return ret;
			pfsm = (IFSM*)pfsm->parent;
		}
		return 0;
	}

	const CFSMState		*FindState( HASH nameHash ) const
	{
		const CFSMState	*ret;
		const IFSM		*pfsm = this;

		while( pfsm )
		{
			ret = (const CFSMState*)pfsm->states.FindByHash(nameHash);
			if( ret )
				return ret;
			pfsm = (IFSM*)pfsm->parent;
		}
		return 0;
	}

	const SFSMCommand	*FindEventHandler( const CFSMState *pState, HASH nameHash ) const
	{
		const SFSMCommand	*ret;
		while( pState )
		{
			ret = (const SFSMCommand*)pState->events.FindByHash(nameHash);
			if( ret )
				return ret;
			if( pState->isOrphan )
				break;
			pState = pState->parent;
		}
		return 0;
	}
};

#pragma pack(pop)
#endif
